<%= render :partial => 'spree/admin/shared/order_tabs', :locals => { :current => 'Customer Returns' } %>

<% admin_breadcrumb("#{Spree.t(:editing_reimbursement)} #{@reimbursement.number}") %>


<% content_for :page_actions do %>
  <li><%= button_link_to Spree.t(:back_to_customer_return), url_for([:edit, :admin, @order, @reimbursement.customer_return]) %></li>
<% end %>

<%= render :partial => 'spree/shared/error_messages', :locals => { :target => @reimbursement } %>

<%= form_for [:admin, @order, @reimbursement] do |f| %>
  <fieldset class='no-border-bottom'>
    <legend align='center'><%= Spree.t(:items_to_be_reimbursed) %></legend>
    <table class="index reimbursement-return-items">
      <thead>
        <tr>
          <th><%= Spree::Product.model_name.human %></th>
          <th><%= Spree::ReturnItem.human_attribute_name(:preferred_reimbursement_type_id) %></th>
          <th><%= Spree::ReturnItem.human_attribute_name(:override_reimbursement_type_id) %></th>
          <th><%= Spree::ReturnItem.human_attribute_name(:amount) %></th>
          <th><%= Spree::ReturnItem.human_attribute_name(:total) %></th>
          <th><%= Spree::ReturnItem.human_attribute_name(:exchange_variant) %></th>
        </tr>
      </thead>
      <tbody>
        <%= f.fields_for :return_items, @reimbursement.return_items.sort_by(&:id) do |item_fields| %>
          <% return_item = item_fields.object %>

          <tr>
            <td>
              <div class="variant-name"><%= return_item.inventory_unit.variant.name %></div>
              <div class="variant-options"><%= return_item.inventory_unit.variant.options_text %></div>
            </td>
            <td class="align-center">
              <%= reimbursement_type_name(return_item.preferred_reimbursement_type) %>
            </td>
            <td class='align-center'>
              <%= item_fields.select(:override_reimbursement_type_id,
                reimbursement_types.collect { |r| [r.name.humanize, r.id] },
                {include_blank: true},
                {class: 'select2 fullwidth'}
              ) %>
            </td>
            <td class="align-center">
              <%= item_fields.text_field :amount, { class: 'refund-amount-input' } %>
            </td>
            <td class="align-center">
              <%= return_item.display_total %>
            </td>
            <td class="align-center">
              <% if return_item.exchange_processed? %>
                <%= return_item.exchange_variant.exchange_name %>
              <% else %>
                <%= item_fields.collection_select :exchange_variant_id, return_item.eligible_exchange_variants(@stock_locations), :id, :exchange_name, { include_blank: true }, { class: "select2 fullwidth return-item-exchange-selection" } %>
              <% end %>
            </td>
          </tr>

        <% end %>
      </tbody>
    </table>
  </fieldset>

  <div class="form-buttons filter-actions actions" data-hook="buttons">
    <%= f.button do %>
      <%= Spree.t(:update) %>
    <% end %>
  </div>
  <br/>
<% end %>

<fieldset>
  <legend align='center'><%= Spree.t(:calculated_reimbursements) %></legend>
  <table class="index calculated-reimbursements">
    <thead data-hook="customer_return_header">
      <tr>
        <th><%= Spree::ReimbursementType.model_name.human %></th>
        <th><%= Spree.t(:description) %></th>
        <th><%= Spree::Reimbursement.human_attribute_name(:total) %></th>
      </tr>
    </thead>
    <tbody>
      <% @reimbursement_objects.each do |reimbursement_object| %>
        <tr id="<%= spree_dom_id(reimbursement_object) %>" data-hook="reimbursement_reimbursement_object_row" class="<%= cycle('odd', 'even')%>">
          <td><%= reimbursement_object.class.name.demodulize %></td>
          <td><%= reimbursement_object.description %></td>
          <td><%= reimbursement_object.display_amount %></td>
        </tr>
      <% end %>
    </tbody>
  </table>
  <% if @order.has_non_reimbursement_related_refunds? %>
    <span class="red">
      <%= Spree.t(:note_already_received_a_refund) %>
    </span>
  <% end %>
  <div class="form-buttons filter-actions actions" data-hook="reimburse-buttons">
    <% if !@reimbursement.reimbursed? %>
      <%= button_to [:perform, :admin, @order, @reimbursement], { class: 'button', method: 'post' } do %>
        <%= Spree.t(:reimburse) %>
      <% end %>
      <%= button_link_to Spree.t('actions.cancel'), url_for([:admin, @order, @reimbursement.customer_return]) %>
    <% end %>
  </div>
</fieldset>
